package com.data.platform.banner.runner;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.annotation.Order;

import java.util.concurrent.TimeUnit;

@Slf4j
@Order(9999)
public class BannerApplicationRunner implements ApplicationRunner {
    private static final String URL_PREFIX = "http://127.0.0.1:";

    @Autowired
    private ServerProperties serverProperties;
    @Value("${spring.application.name}")
    private String name;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        StringBuilder builder = new StringBuilder(URL_PREFIX + serverProperties.getPort());
        String contextPath = serverProperties.getServlet().getContextPath();
        if (StrUtil.isNotBlank(contextPath)) {
            if (! contextPath.startsWith("/")) {
                builder.append("/");
            }
            builder.append(contextPath);
        }
        builder.append("/doc.html");
        ThreadUtil.execute(() -> {
            ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒，保证输出到结尾
            log.info("\n----------------------------------------------------------\n\t" +
                            "(♥◠‿◠)ﾉﾞ  {}启动成功   ლ(´ڡ`ლ)ﾞ\n\t" +
                            "接口文档:  {} \n" +
                            "----------------------------------------------------------",
                    name, builder);
        });

    }
}
